//
//  ZRHomeViewController.swift
//  Sina
//
//  Created by 张锐 on 16/2/24.
//  Copyright © 2016年 zhangr. All rights reserved.
//

import UIKit
import AFNetworking
let CZHomeViewControllerReuserIdenfiter = "CZHomeViewControllerReuserIdenfiter"
class ZRHomeViewController: ZRBaseViewController {
    
     lazy var homeVM = ZRHomeViewModel()
    
    var refreshView : ZRRefreshView?
    override func viewDidLoad() {
        super.viewDidLoad()
        
        visitor?.settingUI("visitordiscover_feed_image_house", text: "关注一些人，回这里看看有什么惊喜", animation: true)
        // Do any additional setup after loading the view.
        
        
        //判断是拜访者视图还是tab视图
        if ZRTokenModelView.shareInstance.isLogin! {
            loadData()
//            homeVM.loadData({ () -> () in
//                
//                //刷新界面
//                self.tableView.reloadData()
//            })
            
            tableView.registerClass(ZRHomeTableViewCell.self, forCellReuseIdentifier: CZHomeViewControllerReuserIdenfiter)
            
            self.tableView.rowHeight = UITableViewAutomaticDimension

            self.tableView.estimatedRowHeight = 200
            
            self.tableView.tableFooterView = pullView
            
//            self.tableView.rowHeight = 200
            
            
            //添加下拉刷新控件
             refreshView = ZRRefreshView()
            
            tableView.addSubview(refreshView!)
            
            refreshView!.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
            
            
        }
    }
    /**
     初始化或上拉刷新加载数据
     */
    func loadData(){
        
        var since_id = homeVM.array.first?.id ?? 0
        
        var max_id = 0
        
        if pullView.isAnimating(){
            
            since_id = 0
        
            max_id = homeVM.array.last?.id ?? 0
            
            max_id -= 1
        
        }
        homeVM.loadData(since_id, max_id: max_id) { (i : Int) -> () in
            
            self.tableView.reloadData()
            
            self.pullView.stopAnimating()
            
            if since_id != 0{
            

                //完成回调恢复
                self.refreshView?.endRefreshView()
            
            
                //提醒微博更新
         
            
                let messageLabel = UILabel()
                
                messageLabel.frame = CGRectMake(0, 20, UIScreen.mainScreen().bounds.width, 44)
                
                messageLabel.backgroundColor = UIColor.orangeColor()
                
                messageLabel.textAlignment = .Center
                
                messageLabel.textColor = UIColor.blackColor()
                
                if i == 0{
                
                    messageLabel.text = "暂无更新"
                    
                }else{
                
                    messageLabel.text = "\(i)条数据更新"
                
                }
                
                self.navigationController?.view.insertSubview(messageLabel, belowSubview: (self.navigationController?.navigationBar)!)
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                    messageLabel.transform = CGAffineTransformMakeTranslation(0, 44)
                    
                    }, completion: { (_) -> Void in
                        
                    UIView.animateWithDuration(1, animations: { () -> Void in
                        messageLabel.transform = CGAffineTransformIdentity
                        }, completion: { (_) -> Void in
                            
                            messageLabel.removeFromSuperview()
                    })
                })
                
                
            
            
            }
            
        }
    
    
    }
    /// 懒加载菊花
    private lazy var pullView : UIActivityIndicatorView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)
  
}
// MARK: - 数据源方法
extension ZRHomeViewController{

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
      
        
        return homeVM.array.count
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(CZHomeViewControllerReuserIdenfiter, forIndexPath: indexPath) as! ZRHomeTableViewCell
        
        let cellModel : ZRHomeCellViewModel = ZRHomeCellViewModel()
        
        cellModel.statusModel = homeVM.array[indexPath.row]
        
        cell.statusViewModel = cellModel
        
        //判断最后cell开始发送网络数据加载更多数据，并且为了防止加载数据这段时间来回滑动多次发送数据请求，通过菊花判断
        if indexPath.row == homeVM.array.count - 1 && !pullView.isAnimating(){
        
            pullView.startAnimating()
            
            loadData()
        
        }
        
        return cell
        
        
    }


}
